package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.exception.CustomerException;
import com.example.service.AdminService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody Account account){
        Account dbAccount=null;
        if("ADMIN".equals(account.getRole())){
            dbAccount=adminService.login(account);
        }else if ("USER".equals(account.getRole())){
            dbAccount=userService.login(account);
        }else {
            throw new CustomerException("非法请求");
        }

        return Result.success(dbAccount);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
       userService.register(user);
        return Result.success();
    }


    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account){
        if("ADMIN".equals(account.getRole())){
            adminService.updatePassword(account);
        }
        if("USER".equals(account.getRole())){
            userService.updatePassword(account);
        }
        return Result.success();
    }
}
